var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    productName: "",
    productModel: [],
    isshowla: false
  },
  bindprivew(e) {
    console.log(e)
    let index = e.currentTarget.dataset.index
    let indexImage = e.currentTarget.dataset.indexImage
    let productImage = this.deepCopy(this.data.productModel[index].productImage)
    productImage.forEach(item => {
      item.image = this.data.Imageurl + item.image
    })
    let newproductImage = productImage.map(item => item.image)
    let t = this
    wx.previewImage({
      urls: newproductImage,
      current: newproductImage[indexImage],
    })
  },
  determine() {
    let productModel = this.data.productModel
    let productModeltrue =productModel.filter(item=>item.isshow)
    console.log(productModeltrue)
    // productModel.forEach(item => {
    //   console.log(item.productImage.some(item1 => item1.ischeck))
    //   if (item.productImage.some(item1 => item1.ischeck)) {
    //     productModeltrue.push(item)
    //   }
    // })
    console.log(productModeltrue)
    let pages = getCurrentPages();
    let prevPage = pages[pages.length - 2]; //prevPage 是获取上一个页面的js里面的pages的所有信息。 -2 是上一个页面，-3是上上个页面以此类推。\\
    let product = this.deepCopy(prevPage.data.product)

    productModeltrue.forEach((item, index) => {
      if (index == 0) {
        let obj = {
          specificationsobj: {
            name: item.productSpecName,
            id: item.productSpecId,
          },
          productSeriestwoobj: {
            name: item.productSeriesName,
            id: item.productSeriesId,
          },
          productModelobj: {
            name: item.model+item.name,
            id: item.id,
          },

          productSpec: [{
            name: item.productSpecName,
            id: item.productSpecId,
            productModel: [{
              id: item.id,
              model: item.model,
              name: item.name,
              productImage: item.productImage
            }]
          }],
          productModel: [{
            name: item.productSpecName,
            model: item.mode,
            id: item.productSpecId,
            productImage: item.productImage
          }],
          newproductModel: [item.model + item.name],
          productImage: item.productImage,
          number: '',

        }


        product[this.data.productindex] = obj
      } else {
        let obj = {
          specificationsobj: {
            name: item.productSpecName,
            id: item.productSpecId,
          },
          productSeriestwoobj: {
            name: item.productSeriesName,
            id: item.productSeriesId,
          },
          productModelobj: {
            name: item.model+item.name,
            id: item.id,
          },

          productSpec: [{
            name: item.productSpecName,
            id: item.productSpecId,
            productModel: [{
              id: item.id,
              model: item.model,
              name: item.name,
              productImage: item.productImage
            }]
          }],
          productModel: [{
            name: item.productSpecName,
            model: item.model,
            id: item.productSpecId,
            productImage: item.productImage
          }],
          newproductModel: [item.model + item.name],
          productImage: item.productImage,
          number: '',

        }
        product.push(obj)
      }

    })





    prevPage.setData({
      product: product
    })
    console.log(prevPage.data.product)
    wx.navigateBack({
      delta: 1,
    })

  },
  // 因为会改变原数组,所以进行深拷贝
  deepCopy(data) {

    if (data && typeof data === "object") {
      //针对函数的拷贝
      if (typeof data === "function") {
        let tempFunc = data.bind(null);
        tempFunc.prototype = deepCopy(data.prototype);
        return tempFunc;
      }

      switch (Object.prototype.toString.call(data)) {
        case "[object String]":
          return data.toString();
        case "[object Number]":
          return Number(data.toString());
        case "[object Boolean]":
          return new Boolean(data.toString());
        case "[object Date]":
          return new Date(data.getTime());
        case "[object Array]":
          var arr = [];
          for (let i = 0; i < data.length; i++) {
            arr[i] = this.deepCopy(data[i]);
          }
          return arr;

          //js自带对象或用户自定义类实例
        case "[object Object]":
          var obj = {};
          for (let key in data) {
            //会遍历原型链上的属性方法，可以用hasOwnProperty来控制 （obj.hasOwnProperty(prop)
            obj[key] = this.deepCopy(data[key]);
          }
          return obj;
      }
    } else {
      //string,number,bool,null,undefined,symbol
      return data;
    }
  },
  bindconfirm() {
    let t = this
    wx.showLoading({
      title: '加载中...', //提示的内容
      mask: true //显示透明蒙层 防止触摸穿透
    })
    wx.request({
      url: app.globalData.Interfaceurl + 'getProductSpec',
      method: "post",
      header: {
        "content-type": "application/x-www-form-urlencoded;charset=UTF-8"
      },
      data: {
        // targetUserId: app.globalData.userId,
        userId: 1569562832576524288,
        name: t.data.productName,
      },
      success: res => {
        res.data.productModel.forEach(item => {
          item.isshow = false,
            item.productImage.forEach(item1 => {
              item1.ischeck = true
            })
        })
        t.setData({
          productModel: res.data.productModel
        })
        wx.hideLoading();
      },
      fail: err => {
        wx.showToast({
          title: '请求失败',
          duration: 2500, //持续的时间
          icon: 'none', //图标
        })
      }
    })
  },
  bindshow(e) {
    let eindex = e.currentTarget.dataset.index
    let productModel = this.deepCopy(this.data.productModel)
    productModel[eindex].isshow = !productModel[eindex].isshow
    if(productModel.some(item=>item.isshow)){
      this.setData({
        isshowla: true
      })
    }else{
      this.setData({
        isshowla: false
      })
    }
    this.setData({
      productModel: productModel
    })
  },
  bindcheckout(e) {

    let eindex = e.currentTarget.dataset.index
    let indexImage = e.currentTarget.dataset.indeximage
    let productModel = this.deepCopy(this.data.productModel)
    // console.log(productModel[eindex].productImage[indexImage])

    productModel[eindex].productImage[indexImage].ischeck = !productModel[eindex].productImage[indexImage].ischeck

    let isshowla = false
    productModel.forEach(item => {
      if (item.productImage.some(item1 => item1.ischeck == true)) {
        isshowla = true
      }
    })



    this.setData({
      productModel: productModel,
      isshowla: isshowla
    })




  },
  fakeCallback() {},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      Imageurl: app.globalData.Imageurl,
      productindex: options.productindex
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})